/**
 * 
 */
package edu.sjtu.se.prophet.io.gson;

import java.lang.reflect.Type;
import java.util.Map;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import edu.sjtu.se.prophet.graph.Edge;
import edu.sjtu.se.prophet.graph.Node;

/**
 * @author willard
 * 
 */
public class EdgeDeserializer implements JsonDeserializer<Edge> {

    @Override
    public Edge deserialize(JsonElement json, Type type,
            JsonDeserializationContext context) throws JsonParseException {
        Node startNode = null;
        Node endNode = null;
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject()
                .entrySet()) {
            if ("start".equals(entry.getKey())) {
                startNode = new Node(entry.getValue().getAsString());
            } else if ("end".equals(entry.getKey())) {
                endNode = new Node(entry.getValue().getAsString());
            }
        }
        return new Edge(startNode, endNode);
    }

}
